/*
 * Copyright 2010 Daniel Kurka
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package it.davial.mgwt.client.ui.widget.cell;

import it.davial.mgwt.client.Visit;
import it.davial.mgwt.client.resources.ResourceAware;
import it.davial.mgwt.client.utils.SharedUtils;

import org.bsc.mgwt.client.widget.celllist.EditableCell;

import com.google.gwt.core.client.GWT;
import com.google.gwt.safehtml.client.SafeHtmlTemplates;
import com.google.gwt.safehtml.shared.SafeHtml;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;


/**
 * @author Daniel Kurka
 *
 */
public class VisitCell extends EditableCell<Visit> implements ResourceAware {

	private static Template TEMPLATE = GWT.create(Template.class);
	private boolean edit = false;
	
	public final boolean isEdit() {
		return edit;
	}



	public final void setEdit(boolean edit) {
		this.edit = edit;
	}

	private final static String customerTemplate = 
			"<div class=\"{0}\">" +
				"<p class=\"{1}\">{3}</p>" +
				"<p class=\"{2}\">{4}</p>" +
			"</div>";
		
	public interface Template extends SafeHtmlTemplates {
		@SafeHtmlTemplates.Template(customerTemplate)
		SafeHtml searchPlace(String styleNoteContainer,
			     String styleTitle,
			     String styleDate,
			     String name,
			     String address);
	}



	@Override
	public void render(SafeHtmlBuilder safeHtmlBuilder, Visit model) {	
		
		
		String desc = SharedUtils.isNullOrEmpty(model.getDescVisit()) ? "" : model.getDescVisit();
		String diagn = SharedUtils.isNullOrEmpty(model.getDiagnosticVisit()) ? "" : model.getDiagnosticVisit();
		String patientId = model.getIdPatient();
		String data = SharedUtils.isNullOrEmpty(model.getDataVisit()) ? "" : model.getDataVisit();
		
		safeHtmlBuilder.append(TEMPLATE.searchPlace(
				 mainCss.getCustomerCell().customerContainer(),
				 mainCss.getCustomerCell().name(),
				 mainCss.getCustomerCell().address(),
				 desc.concat(" ").concat(data),
				 patientId.concat(" ").concat(diagn)
));


	}



	@Override
	public boolean canBeSelected(Visit model) {
		// TODO Auto-generated method stub
		return !isEdit();
	}


	@Override
	public Style getStyle( int index, Visit model  ) {
		Style result =  edit ? Style.EDIT : Style.NONE;	
		
		return result;
	}



	@Override
	public boolean isEditable(int index, Visit model) {
		// TODO Auto-generated method stub
		return isEdit();
	}
	
	

}
